package lixp.rabbitmqAdvance.producer.service;

import lixp.rabbitmqAdvance.producer.utils.OKHttpClientUtil;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import static java.lang.System.out;

@Service
public class ProCreateService {

    @Autowired
    private RabbitAdmin rabbitAdmin;

    public void createMQIfNotExist(String queueName ,String exchangeName) {
        //判断队列是否存在
        Properties properties = rabbitAdmin.getQueueProperties(queueName);
        if(properties == null){
            Queue queue = new Queue(queueName, true, false, false, null);
            FanoutExchange fanoutExchange = new FanoutExchange(exchangeName);
            rabbitAdmin.declareQueue(queue);
            rabbitAdmin.declareExchange(fanoutExchange);
            rabbitAdmin.declareBinding(BindingBuilder.bind(queue).to(fanoutExchange));
            //新启动一个线程，通知消费者新增listener
            new Thread(new Runnable() {
                @Override
                public void run() {
                    String res = callAddNewListener(queueName);

                    if(!StringUtils.isEmpty(res)){
                        out.println("-->>调用创建新的 listener feign 失败");
                    }
                }
            }).start();

        }

    }

    private String callAddNewListener(String queueName){
        out.println(queueName);
        String url = "http://localhost:8020/add_new_listener";
        Map<String,String> param = new HashMap<String,String>();
        param.put("queueName",queueName);
        try {
            OKHttpClientUtil.doGet(url,null,param);
        }catch (Exception e){
            e.printStackTrace();
            return "调用添加listener feign失败";
        }
        return null;

    }

}


